package com.taobao.system.controller;
import com.taobao.basic.util.JsonResult;
import com.taobao.basic.util.PageList;
import com.taobao.system.domain.SystemDictionaryDetail;
import com.taobao.system.query.SystemDictionaryDetailQuery;
import com.taobao.system.service.ISystemDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // controller层，返回json格式数据
@RequestMapping("/detail")
@Api(value = "数据字典接口类",description = "数据字典接口的CRUD操作")
public class SystemDictionaryDetailController {

    @Autowired
    private ISystemDictionaryDetailService SystemDictionaryDetailService;

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<SystemDictionaryDetail> findAll(){
        return SystemDictionaryDetailService.loadAll();
    }
    /**
     * 根据id查询单个数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public SystemDictionaryDetail findOne(@PathVariable("id") Long id){
        return SystemDictionaryDetailService.loadById(id);
    };
    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public JsonResult DeleteByID(@PathVariable("id") Long id){
        try {
            SystemDictionaryDetailService.remove(id);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.me().setMsg("网络加载失败，请稍后重试！！");
        }
    }
    /**
     * 添加或修改操作
     * @param SystemDictionaryDetail
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口")
    public JsonResult updateOrAdd(@RequestBody SystemDictionaryDetail SystemDictionaryDetail ){
        try {
            if (SystemDictionaryDetail.getId() == null){
                SystemDictionaryDetailService.save(SystemDictionaryDetail);
            }else {
                SystemDictionaryDetailService.update(SystemDictionaryDetail);
            }
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     *
     */
    @PostMapping()
    @ApiOperation(value = "分页查询接口")
    // @RequestBody -从请求体中获取数据赋值给SystemDictionaryDetailQuery
    public PageList<SystemDictionaryDetail> pageQuery(@RequestBody SystemDictionaryDetailQuery query){
        try {
            return SystemDictionaryDetailService.pageQuery(query);
        }catch (Exception e){
            e.printStackTrace();
            return new PageList<SystemDictionaryDetail>();
        }
    }
    /**
     * 批量删除
     * @param
     * @return
    */
    @PatchMapping()
    @ApiOperation(value = "删除接口")
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            SystemDictionaryDetailService.patchDel(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.me().setMsg("网络加载失败，请稍后重试！！");
        }
    }

    /**
     * 接口：根据前端传过来的type_id来查询对应的对象
     * @param types_id
     * @return
     */
    @DeleteMapping("/findBy/{types_id}")
    public List<SystemDictionaryDetail> findByTypeId(@PathVariable Long types_id) {

        return SystemDictionaryDetailService.findByTypeId(types_id);
    }


}
